package com.itcms.core.web.back;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itcms.common.web.Servlets;
import com.itcms.core.constant.Constants;
import com.itcms.core.domain.Node;
import com.itcms.core.domain.User;
import com.itcms.core.service.NodeQueryService;
import com.itcms.core.service.NodeService;
import com.itcms.core.service.UserService;
import com.itcms.core.support.Context;
import com.itcms.webservice.InfopubService;
import com.itcms.webservice.domain.NodeVo;

/**
 * NodeController
 * 
 * @author mengjie
 * 
 */
@Controller
@RequestMapping("/core/node/api")
public class NodeApiController {
	private static final Logger logger = LoggerFactory.getLogger(NodeApiController.class);

	private UserService userService;

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	@RequestMapping("list.do")
	@ResponseBody
	public List apiList(Integer queryParentId, @RequestParam(defaultValue = "false") boolean showDescendants,
			@PageableDefault(sort = { "treeNumber", "id" }, direction = Direction.ASC) Pageable pageable,
			HttpServletRequest request, org.springframework.ui.Model modelMap) {
		List nodeList = new ArrayList();
//		User user = Context.getCurrentUser();
		User user = userService.get(1);
		Integer siteId = Context.getCurrentSiteId();
		Node parent = null;
		if (queryParentId != null) {
			parent = query.get(queryParentId);
		}
		if (parent == null) {
			parent = query.findRoot(siteId);
			if (parent != null) {
				queryParentId = parent.getId();
			}
		}
		String treeNumber = null;
		if (showDescendants && parent != null) {
			treeNumber = parent.getTreeNumber();
		}
		boolean isAllNodePerm = user.getAllNodePerm(siteId);
		Map<String, String[]> params = Servlets.getParamValuesMap(request, Constants.SEARCH_PREFIX);
		List<Node> list = query.findList(siteId, treeNumber, queryParentId, user.getId(), isAllNodePerm, params,
				pageable.getSort());
		for (Node node : list) {
			NodeVo nodeVo = new NodeVo();
			Integer cateId = node.getId();
			Integer leve = node.getTreeLevel();
			Integer cateParentId;
			if(leve==0){
				cateParentId=null;
			}else{
				cateParentId = node.getParent().getId();
			}
		
			String cateName = node.getName();
			
			nodeVo.setCateId(cateId.toString());
			nodeVo.setCateName(cateName);
			String cateParentIdString = null;
			if(!org.springframework.util.StringUtils.isEmpty(cateParentId)){
				cateParentIdString = cateParentId.toString();
			}
			nodeVo.setCateParentId(cateParentIdString);
			nodeVo.setLeve(leve.toString());
			nodeList.add(nodeVo);
		}
		return nodeList;
	}
	
	@Autowired
	@Qualifier("demoClient")
	private InfopubService infopubService;
	
	@RequestMapping("list2.do")
	@ResponseBody
	public void testName() throws Exception {
//		List list = infopubService.getCategoryJson("");
//		System.out.println(list.get(0));
	}
	
	@Autowired
	private NodeService service;
	@Autowired
	private NodeQueryService query;
}
